Skip to content

Fix DB integration quality: dependency injection, idempotent seeding, price precision, and test isolation#8

Merged
goldlabelapps merged 3 commits intostagingfrom
copilot/sub-pr-7
Mar 19, 2026
Merged

Fix DB integration quality: dependency injection, idempotent seeding, price precision, and test isolation#8
goldlabelapps merged 3 commits intostagingfrom
copilot/sub-pr-7

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 19, 2026

Addresses all review comments on the PostgreSQL integration PR: per-request DB connections leaking, duplicate router declaration, ON CONFLICT DO NOTHING being a no-op (no unique constraint), price losing precision via float(), scripts executing at import time, missing dependencies in requirements.txt, and tests requiring a live DB.

API / Routes (app/api/routes.py)

  • Remove duplicate router = APIRouter()
  • Move load_dotenv() to module level
  • Extract DB setup into a get_db_connection() generator dependency injected via Depends() — connection lifecycle guaranteed by try/finally
  • Price serialised as str(row[3]) instead of float(row[3]) to preserve NUMERIC exactness
def get_db_connection():
    conn = psycopg2.connect(...)
    try:
        yield conn
    finally:
        conn.close()

@router.get("/")
def root(conn=Depends(get_db_connection)) -> dict:
    ...

Seeding (app/seed_product_table.py)

  • Add UNIQUE constraint on name so ON CONFLICT (name) DO NOTHING is a real conflict target — re-running the script is now idempotent
  • Wrap execution in main() + if __name__ == "__main__": guard

Utility scripts (app/print_products.py, app/test_db_connection.py)

  • Wrap in main() + guard to prevent side effects on import
  • test_db_connection.py returns a non-zero exit code on failure via sys.exit()

Dependencies (requirements.txt)

  • Add python-dotenv>=1.0.0 and psycopg2-binary>=2.9.0

Metadata (app/main.py)

  • Source FastAPI version from app.__version__ instead of the hard-coded "1.0.0"

Tests (tests/test_routes.py)

  • Override get_db_connection with a mock dependency — tests run without a live PostgreSQL instance
  • Replace stale test_root_returns_welcome_message with tests validating the {meta, data} response shape; mock rows use Decimal to accurately reflect DB types

📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

Copilot AI and others added 2 commits March 19, 2026 19:40
…ing, main() guards, precision fix, tests

Co-authored-by: goldlabelapps <244006120+goldlabelapps@users.noreply.github.com>
Copilot AI changed the title [WIP] Add PostgreSQL database integration for product data management Fix DB integration quality: dependency injection, idempotent seeding, price precision, and test isolation Mar 19, 2026
Copilot AI requested a review from goldlabelapps March 19, 2026 19:41
Copy link
Copy Markdown
Owner

@goldlabelapps goldlabelapps left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@goldlabelapps goldlabelapps merged commit c07b4ea into staging Mar 19, 2026
0 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants